'use strict';

const Service = require('egg').Service;
const utility = require("utility") //密码加密
/**
 * 添加用户
 *
 * @class AddService
 * @extends {Service}
 */
class AddService extends Service {
    async addUser(obj) {
        const { ctx } = this;
        let { userName, passWord, role } = obj;
        return new Promise(async (resolve, reject) => {
            let FindArr = await ctx.model.User.findOne({ userName: userName });
            // console.log(FindArr)
            if (FindArr) return resolve({ code: 201, msg: '用户以存在' });
            await this.ctx.model.User.create({
                userName: userName,
                passWord: await utility.md5(passWord),
                role
            }).then(async data => {
                // console.log(data)
                if (!data) return resolve({ code: 201, msg: '用户创建失败' });
                return resolve({ code: 200, msg: '用户创建成功' });
            })
        })

    }
    /**
     *添加会员
     *
     * @param {*} obj
     * @returns
     * @memberof AddService
     */
    async addVip(obj) {
        const { ctx } = this;
        let { userName, tell, passWord, roleClass, currency, desc } = obj;

        console.log(obj)
        return new Promise(async (resolve, reject) => {
            let FindArr = await ctx.model.Vip.findOne({ tell: tell });
            console.log(FindArr)
            if (FindArr) return resolve({ code: 201, msg: '会员用户以存在' });
            let objt = {}
            if (obj.desc == '') {
                objt = {
                    userName: userName,
                    tell,
                    passWord: await utility.md5(passWord),
                    roleClass,
                    currency,
                    accruingAmounts: currency
                }
            } else {
                objt = {
                    userName: userName,
                    tell,
                    passWord: await utility.md5(passWord),
                    roleClass,
                    currency,
                    // 累计充值金额
                    accruingAmounts: currency,
                    desc
                }
            }
            await this.ctx.model.Vip.create(objt).then(async data => {
                // console.log(data)
                if (!data) return resolve({ code: 201, msg: '会员用户创建失败' });
                return resolve({ code: 200, msg: '会员用户创建成功' });
            })
        })
    }
    /**
     *添加消费项目 
     *
     * @param {*} obj
     * @returns
     * @memberof AddService
     */
    async addConsume(obj) {
        const { ctx } = this;
        let { orderName, price, desc } = obj;
        return new Promise(async (resolve, reject) => {
            let FindArr = await ctx.model.Order.findOne({ orderName: orderName });
            // // console.log(FindArr)
            if (FindArr) return resolve({ code: 201, msg: '项目以存在' });
            await this.ctx.model.Order.create({
                orderName,
                price: JSON.stringify(price),
                desc
            }).then(async data => {
                // console.log(data)
                if (!data) return resolve({ code: 201, msg: '用户创建失败' });
                return resolve({ code: 200, msg: '用户创建成功' });
            })
        })
    }
    /**
     *添加订单
     *
     * @param {*} obj
     * @returns
     * @memberof AddService
     */
    async addConsumerOrders(obj) {
        const { ctx } = this;
        let { userName, tell, roleClass, orderArr, price, desc, operation, role } = obj;
        return new Promise(async (resolve, reject) => {

            // 修改vip用户余额
            let find = await this.ctx.model.Vip.findOne({ userName });
            // console.log(find)
            let n = find.currency * 100000 - price * 100000;
            let num = n / 100000;
            if (num <= 0) return resolve({ code: 201, msg: '会员余额不足！' });
            await this.ctx.model.Vip.updateOne({ userName }, { currency: num });
            // 创建订单
            await this.ctx.model.ConsumeOrders.create({
                userName, tell, roleClass, orderArr, price, desc, operation, role
            }).then(async data => {
                // console.log(data)
                if (!data) return resolve({ code: 201, msg: '创建订单失败' });
                return resolve({ code: 200, msg: '创建订单成功' });
            })
        })
    }

    /**
     *充值
     *
     * @param {*} obj
     * @returns
     * @memberof AddService
     */
    async addvipTapUp(obj) {
        const { ctx } = this;
        let { userName, tell, roleClass, topUp, desc, operation, role } = obj;
        return new Promise(async (resolve, reject) => {

            // 修改vip用户余额
            let find = await this.ctx.model.Vip.findOne({ userName });
            // console.log(find)
            let n = find.currency * 100000 + topUp * 100000;
            let m = find.accruingAmounts * 100000 + topUp * 100000;
            let currency = n / 100000;
            let accruingAmounts = m / 100000;
            let roleClass = 1;
            // 会员自动升级
            if(accruingAmounts >= 1000) roleClass = 2;
            if(accruingAmounts >= 10000) roleClass = 3;
            let up = await this.ctx.model.Vip.updateOne({ userName }, { currency, accruingAmounts, roleClass });
            if(up.n != 1 && up.ok != 1) return resolve({ code: 201, msg: '充值异常' });
            // 创建订单
            await this.ctx.model.TapUp.create({
                userName, tell, roleClass, topUp, desc, operation, role
            }).then(async data => {
                // console.log(data)
                if (!data) return resolve({ code: 201, msg: '创建订单失败' });
                return resolve({ code: 200, msg: '创建订单成功' });
            })
        })
    }

    // userName: "",
    //     tell: "",
    //     roleClass: 1,
    //     // 充值金额
    //     topUp: 0,
    //     // 充值说明
    //     desc: "",
    //     // 操作者
    //     operation: "",
    //     role: 0

    
}

module.exports = AddService;
